﻿using BLL;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Dryricenet
{
    
    public partial class home__Password : System.Web.UI.Page
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //给button1添加客户端事件
                //Button2.Attributes.Add("OnClick", "return settime(this)");

            }
        }
        //获取手机验证码
        private bool CallPhone(string Recipient_Mobile_Num)
        {
            //
            string PostUrl = "http://106.ihuyi.com/webservice/sms.php?method=Submit";
            //登录“互亿无线网站”查看用户名 登录用户中心->验证码通知短信>产品总览->API接口信息->APIID
            string account = "C12590485";
            //登录“互亿无线网站”查看密码 登录用户中心->验证码通知短信>产品总览->API接口信息->APIKEY
            string password = "0c3d73e95c4ac6363efe94b37ea7e8ed";


            ;
            //接收短信的用户的手机号码
            string mobile = Recipient_Mobile_Num;
            //随机生成四位数 可以模仿向用户发送验证码
            Random rad = new Random();
            int mobile_code = rad.Next(1000, 10000);   //生成随机数 
            string content = "您的验证码是：" + mobile_code + " 。请不要把验证码泄露给其他人。";
            PhoneVerification.Phonevt = mobile_code.ToString();
            string postStrTpl = "account={0}&password={1}&mobile={2}&content={3}";  //用户名+密码+注册的手机号+验证码

            UTF8Encoding encoding = new UTF8Encoding();  //万国码
            //将 account, password, mobile, content 这四个内容添加到postStrTpl字符串当中
            //并利用encoding.GetBytes()将括号里面的字符串转化为二进制类型
            byte[] postData = encoding.GetBytes(string.Format(postStrTpl, account, password, mobile, content)); //将字符串postStrTpl中的格式项替换为四个个指定的 Object 实例的值的文本等效项。再转为二进制数据

            //新建一个请求对象
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(PostUrl);//对统一资源标识符 (URI) 发出请求。 这是一个 abstract 类。
            myRequest.Method = "POST";
            myRequest.ContentType = "application/x-www-form-urlencoded";
            myRequest.ContentLength = postData.Length;

            Stream newStream = myRequest.GetRequestStream();
            //间postData合并到 PostUrl中去
            newStream.Write(postData, 0, postData.Length);
            newStream.Flush();
            newStream.Close();


            //以http://106.ihuyi.com/webservice/sms.php?method=Submit&account=你的APIID&password=你的APIKEY&mobile=接收短信的用户的手机号码&content=您的验证码是：" + mobile_code + " 。请不要把验证码泄露给其他人。"    发起https请求   并获取请求结果
            HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();

            if (myResponse.StatusCode == HttpStatusCode.OK)
            {
                return true;
            }
            else
            {
                return false;
                //访问失败
            }



        }
        //登录
        protected void Button1_Click1(object sender, EventArgs e)
        {
            if (txtPhone.Text.Trim() != "")
            {
                string phonename = userBLL.phoneUserBLL(txtPhone.Text.Trim()).ToString();

                if (PhoneVerification.Phonevt == txtCode.Text.Trim())
                {
                    if (userBLL.phoneDLBLL(txtPhone.Text.Trim()).Rows.Count > 0)
                    {
                        Session["Name"] = phonename;
                        Response.Redirect("home_Center.aspx");
                    }
                    else
                    {
                        Response.Write("<script>javascript:alert('亲,登录失败了哦!');</script>");
                        Response.End();
                    }
                }
                else
                {
                    Response.Write("<script>javascript:alert('验证码输入错误！');</script>");
                    Response.End();
                }
            }
        }
        //获取验证码
        protected void txtYZM_Click(object sender, EventArgs e)
        {
            if (txtPhone.Text.Trim() != "")
            {
                txtYZM.Text = "正在获取中...";
                ClientScript.RegisterStartupScript(Page.GetType(), "", "settime(this);", true);
                ScriptManager.RegisterStartupScript(txtYZM, this.GetType(), "magin", "settime(this)", true);
                CallPhone(txtPhone.Text.ToString());
            }
            
           


        }
    }
}